package com.ChenYaRou.www.controller;

import com.ChenYaRou.www.constant.JsonBeanConstant;
import com.ChenYaRou.www.dao.*;
import com.ChenYaRou.www.dao.impl.*;
import com.ChenYaRou.www.po.*;
import com.ChenYaRou.www.utils.JsonBean;
import com.ChenYaRou.www.utils.UpdateOrInsertOrSelectMoreAllBean;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static com.ChenYaRou.www.constant.Constant.DELETE;
import static com.ChenYaRou.www.constant.Constant.SHOW;

/**
 * @ClassName PersonalHomePageServletController
 * @Description TODO
 * @Author 86183
 * @Date 2022/5/1 14:13
 * @Version 1.0
 **/
public class PersonalHomePageServletController {

    /**
     * 个人中心的详细用户信息
     * @param userId
     * @return
     */
    public String  personalHomePageServletDetailUser(Integer userId){
        if(userId!=null){
            DetailedUserDao detailedUserDao=new DetailedUserDaoImpl();
            try {
                DetailedUser detailedUser = detailedUserDao.select(userId);
                if(detailedUser!=null ){
                    return JsonBean.generateJson(JsonBeanConstant.GET,JsonBeanConstant.SUCCESS,detailedUser);
                }
            } catch (SQLException | NoSuchFieldException | InstantiationException |
                    IllegalAccessException  e) {
                e.printStackTrace();
            }
        }else{
            return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
        }
        return JsonBean.operateList(JsonBeanConstant.NULL,null);
    }

    /**
     * 个人中心的收藏箱
     * @param userId
     * @param shopId 如果为null则查找全部,否则查找特定店铺
     * @return
     */
    public String personalHomePageServletCollection(Integer userId,Integer shopId){
        if(userId!=null) {
            try {
                Collection collection = new Collection();
                collection.setUserId(userId);
                collection.setState(SHOW);
                if(shopId!=null){
                    collection.setShopId(shopId);
                }
                List<JsonBeanCollection> jsonBeanCollections=new ArrayList<>();

                List<Collection> collections =
                        UpdateOrInsertOrSelectMoreAllBean.getInstance().selectMore(collection);

                if(collections!=null && !collections.isEmpty()) {
                    ShopDao shopDao = new ShopDaoImpl();
                    for (Collection collectionEach : collections) {
                        Shop searchShop=new Shop();
                        searchShop.setId(collectionEach.getShopId());
                        Shop shopGet = shopDao.selectSingle(searchShop);
                        jsonBeanCollections.add(
                                new JsonBeanCollection(collectionEach.getId(),
                                        null,shopGet.getName(),collectionEach.getState(),shopGet.getId()));
                    }
                }
                return JsonBean.operateList(JsonBeanConstant.NULL,jsonBeanCollections);
            } catch (IllegalAccessException | SQLException | NoSuchFieldException | InstantiationException e) {
                e.printStackTrace();
            }
        }
        return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
    }

    /**
     * 订单 包括-未接单订单 已完成订单 全部订单（state=null）
     * @return
     */
    public String personalHomePageOrders(Orders orders){
        if(orders!=null ){
            try {
                List<Orders> ordersAll = UpdateOrInsertOrSelectMoreAllBean.getInstance().selectMore(orders);

                List<JsonBeanOrders> jsonBeanOrders=new ArrayList<>();
                if(ordersAll!=null && !ordersAll.isEmpty()){
                    for (Orders ordersEach : ordersAll) {
                        UserDao userDao=new UserDaoImpl();
                        User user = userDao.select(ordersEach.getBuyerId());
                        ShopDao shopDao=new ShopDaoImpl();
                        Shop search=new Shop();
                        search.setId(ordersEach.getShopId());
                        Shop shopGet = shopDao.selectSingle(search);
                        jsonBeanOrders.add(
                                new JsonBeanOrders(ordersEach.getId(),user.getName(),shopGet.getName(),
                                        ordersEach.getContent(),ordersEach.getBuyerId(),ordersEach.getState(),
                                        ordersEach.getCost(), ordersEach.getUpdateTime(),ordersEach.getOrderTime()));
                    }
                    return JsonBean.operateList(JsonBeanConstant.NULL,jsonBeanOrders);
                }
                return JsonBean.operateList(JsonBeanConstant.NULL,null);
            } catch (IllegalAccessException | SQLException | NoSuchFieldException | InstantiationException e) {
                e.printStackTrace();
            }

        }
        return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
    }

    /**
     * 个人中心中的ticket
     * @param userId
     * @return
     */
    public String personalHomePageTicket(Integer userId){
        if(userId!=null){
            try {
                Ticket ticket=new Ticket();
                ticket.setOwnerId(userId);
                List<Ticket> tickets = UpdateOrInsertOrSelectMoreAllBean.getInstance().selectMore(ticket);

                List<JsonBeanTicketUser> jsonBeanTicketUsers=new ArrayList<>();
                if(tickets!=null && !tickets.isEmpty()){
                    for (Ticket ticketEach : tickets) {
                        Shop search=new Shop();
                        search.setId(ticket.getId());
                        Shop shopGet = new ShopDaoImpl().selectSingle(search);
                        jsonBeanTicketUsers.add(
                                new JsonBeanTicketUser(null,shopGet.getName(),
                                        ticketEach.getState(),ticketEach.getDeadline(),
                                        ticketEach.getDiscount()));
                    }
                }
                return JsonBean.operateList(JsonBeanConstant.NULL,jsonBeanTicketUsers);
            } catch (IllegalAccessException | SQLException | NoSuchFieldException | InstantiationException e) {
                e.printStackTrace();
            }

        }
        return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
    }

    /**
     * personalHomePage中的购物车car
     * @param userId
     * @return
     */
    public String personalHomePageCar(Integer userId){
        if(userId!=null){
            try {
                Car car=new Car();
                car.setUserId(userId);
                List<Car> cars = UpdateOrInsertOrSelectMoreAllBean.getInstance().selectMore(car);
                List<JsonBeanCar> jsonBeanCars=new ArrayList<>();
                if(cars!=null && !cars.isEmpty()){
                    for (Car carEach : cars) {
                        ShopDao shopDao=new ShopDaoImpl();
                        Shop search=new Shop();
                        search.setId(carEach.getShopId());
                        Shop shop = shopDao.selectSingle(search);
                        jsonBeanCars.add(
                                new JsonBeanCar(null,carEach.getContent(),
                                        shop.getName(),carEach.getPrice(),carEach.getState()));
                    }
                }
                return JsonBean.operateList(JsonBeanConstant.NULL,jsonBeanCars);
            } catch (IllegalAccessException | SQLException | NoSuchFieldException | InstantiationException e) {
                e.printStackTrace();
            }

        }
        return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
    }

    /**
     * 更新个人中心的detailUser
     * @param newDetailedUser
     * @param userId
     * @return
     */
    public String updateDetailUser(DetailedUser newDetailedUser,Integer userId){
        if(newDetailedUser!=null) {
            DetailedUser oldDetailedUser = new DetailedUser();
            oldDetailedUser.setUserId(userId);
            String success =
                    UpdateOrInsertOrSelectMoreAllBean.getInstance().update(newDetailedUser, oldDetailedUser);
            if (success != null) {
                return success;
            }
            return JsonBean.operateList(JsonBeanConstant.FAIL_UPDATE, null);
        }
        return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
    }

    /**
     * 个人中心中更新订单状态（即取消订单）
     * @param orders
     * @return
     */
    public String updateOrders(Orders orders){
        if(orders!=null){
            Orders oldOrders=new Orders();
            oldOrders.setId(orders.getId());
            String jsonSuccess = UpdateOrInsertOrSelectMoreAllBean.getInstance().update(orders,oldOrders);
            if(jsonSuccess!=null){
                return jsonSuccess;
            }
            return JsonBean.operateList(JsonBeanConstant.FAIL_UPDATE,null);
        }
        return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
    }

    /**
     * 更新个人中心中的收藏箱的收藏状态 取消收藏
     * @param collection
     * @return
     */
    public String updateCollection(Collection collection) {
        if (collection != null) {
            Collection newCollection = new Collection();
            newCollection.setState(DELETE);
            String success = UpdateOrInsertOrSelectMoreAllBean.getInstance().update(newCollection, collection);
            if(success!=null){
                return success;
            }
            return JsonBean.operateList(JsonBeanConstant.FAIL_UPDATE,null);
        }
        return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
    }

    /**
     * 更新个人中心中的购物车 删除购物车
     * @param car 旧car
     * @return
     */
    public String updateCar(Car car){
        if(car!=null){
            Car newCar=new Car();
            newCar.setState(DELETE);
            String success = UpdateOrInsertOrSelectMoreAllBean.getInstance().update(newCar, car);
            if(success!=null){
                return success;
            }
            return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
        }
        return JsonBean.operateList(JsonBeanConstant.FAIL_UPDATE,null);
    }
}
